<template>
  <div class="menu">
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" v-auth="'role:add'"> 新增角色 </a-button>
        <a-button type="primary" :disabled="!selectKey.length" v-auth="'role:batchdelete'" danger
          @click="handleBatchDelete">
          删除角色
        </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction :actions="[
            {
              onClick: handleLook.bind(null, record),
              label: '查看',
            },
            {
              onClick: handleEdit.bind(null, record),
              label: '编辑',
              disabled: record.typeCode == '0',
              auth: 'role:edit'
            },
            {
              color: 'error',
              onClick: handleDelete.bind(null, record),
              label: '删除',
              disabled: record.typeCode == '0',
              auth: 'role:delete'
            },
          ]" />
        </template>
      </template>
    </BasicTable>
    <MenuModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
import { BasicTable, useTable, TableAction } from '@/components/Table';

import { useModal } from '@/components/Modal';
import MenuModal from './Modal.vue';

import { columns, searchFormSchema } from './index.data';

import { useMessage } from '@/hooks/web/useMessage';

import * as api from '@/api/system/role';
import { ref } from 'vue';

const { createConfirm, createMessage: msg } = useMessage();
const [registerModal, { openModal }] = useModal();
const selectKey = ref([]);
const [registerTable, { reload }] = useTable({
  api: api.list,
  columns,
  formConfig: {
    labelWidth: 120,
    schemas: searchFormSchema,
  },
  isTreeTable: false,
  pagination: true,
  striped: false,
  useSearchForm: true,
  showTableSetting: true,
  bordered: true,
  canResize: false,
  showIndexColumn: false,
  rowKey: 'id',
  rowSelection: {
    type: 'checkbox',
    onChange: (e) => {
      selectKey.value = e;
    },
  },
  actionColumn: {
    width: 140,
    title: '操作',
    dataIndex: 'action',
    // slots: { customRender: 'action' },
    fixed: 'right',
  },
});

function handleCreate() {
  openModal(true, {
    isUpdate: false,
    isLook: false,
  });
}

function handleEdit(record: Recordable) {
  openModal(true, {
    record,
    isUpdate: true,
    isLook: false,
  });
}
function handleLook(record: Recordable) {
  openModal(
    true,
    {
      record,
      isUpdate: true,
      isLook: true,
    },
    true,
  );
}

function handleBatchDelete(record: Recordable) {
  createConfirm({
    title: '删除角色',
    content: '是否要批量删除这些角色',
    iconType: 'warning',
    onOk: async () => {
      await api.del(selectKey.value);
      selectKey.value = [];
      msg.success('删除成功');
      reload();
    },
  });
}

function handleDelete(record: Recordable) {
  createConfirm({
    title: '删除角色',
    content: '确定要删除吗？删除后不可恢复',
    iconType: 'warning',
    onOk: async () => {
      await api.del([record.id]);
      msg.success('删除成功');
      selectKey.value = [];
      reload();
    },
  });
}

function handleSuccess() {
  reload();
}
</script>
